#include"Queue.h"
void QueueInit(Queue* q)
{
	assert(q);
	q->head = q->tail = NULL;
	q->size = 0;
}
void QueueDestroy(Queue * q)
{
	QNode* cur = q->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->head = NULL; q->tail = NULL;
	q->size = 0;

}
void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newdata = (QNode*)malloc(sizeof(QNode));
	if (newdata == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newdata->data = data;
	newdata->next = NULL;
	if (q->tail == NULL)
	{
		q->tail = q->head = newdata;
		q->size++;
	}
	else
	{
		q->tail->next = newdata;
		q->tail = newdata;
		q->size++;
	}
}
void QueuePop(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	if (q->head->next == NULL)
	{
		free(q->head);
		q->head = q->tail=NULL;
	}
	else
	{
		QNode* cur = q->head->next;
		free(q->head);
		q->head = cur;
	}
	q->size--;
}
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->head->data;
}
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->tail->data;
}
int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}
int QueueEmpty(Queue* q)
{
	assert(q);
	return q->head==NULL;
}